from wxauto import *
import time
import os

def send_wx_msg(friend_name, message):
    try:
        # 获取当前微信客户端
        wx = WeChat()
        
        # 先切换到对应的聊天窗口
        if not wx.ChatWith(friend_name):
            print(f"切换到聊天窗口失败: {friend_name}")
            return False
        
        # 发送消息
        wx.SendMsg(message, who=friend_name)
        print(f"消息已成功发送给 {friend_name}")
        return True
        
    except Exception as e:
        print(f"发送失败：{str(e)}")
        return False

# def send_message_and_image(name: str, image_path: str) -> bool:
#     """发送消息和图片到指定微信联系人
    
#     Args:
#         name: 联系人的微信名称
#         image_path: 图片文件的路径
        
#     Returns:
#         bool: 是否发送成功
#     """
#     try:
#         # 获取微信实例
#         wx = WeChat()
        
#         # 先切换到对应的聊天窗口
#         if not wx.ChatWith(name):
#             print(f"切换到聊天窗口失败: {name}")
#             return False
            
#         # 发送图片
#         if not wx.SendFiles(image_path):
#             print(f"发送图片失败: {image_path}")
#             return False
            
#         return True
        
#     except Exception as e:
#         print(f"发送消息失败: {str(e)}")
#         return False
def send_message_and_image(name: str, image_path: str) -> bool:
    """发送消息和图片到指定微信联系人
    
    Args:
        name: 联系人的微信名称
        image_path: 图片文件的路径
        
    Returns:
        bool: 是否发送成功
    """
    try:
        print(f"\n=== 开始发送图片 ===")
        print(f"联系人: {name}")
        print(f"图片路径: {image_path}")
        
        # 验证图片文件是否存在
        if not os.path.exists(image_path):
            print(f"图片文件不存在: {image_path}")
            return False
            
        # 获取微信实例 - 每次都重新获取
        wx = WeChat()
        
        # 添加延时确保微信稳定
        time.sleep(0.5)
        
        # 先切换到对应的聊天窗口
        print(f"正在切换到聊天窗口: {name}")
        if not wx.ChatWith(name):
            print(f"切换到聊天窗口失败: {name}")
            return False
            
        # 再次添加延时确保窗口切换完成
        time.sleep(1)
        
        # 验证当前聊天窗口是否正确
        try:
            current_chat = wx.CurrentChat()
            print(f"当前聊天窗口: {current_chat}")
            if current_chat != name:
                print(f"警告: 当前聊天窗口不匹配，期望: {name}, 实际: {current_chat}")
                # 重新尝试切换
                if not wx.ChatWith(name):
                    print(f"重新切换聊天窗口失败: {name}")
                    return False
                time.sleep(1)
        except Exception as e:
            print(f"获取当前聊天窗口失败: {str(e)}")
        
        # 发送图片
        print(f"正在发送图片: {image_path}")
        success = wx.SendFiles(image_path)
        
        if success:
            print(f"图片发送成功: {name}")
            # 添加发送后延时，避免连续发送问题
            time.sleep(2)
            return True
        else:
            print(f"图片发送失败: {name}")
            return False
            
    except Exception as e:
        print(f"发送图片异常: {str(e)}")
        return False
    finally:
        print("=== 发送图片结束 ===\n")

# 使用示例
if __name__ == "__main__":
    # 要发送消息的好友名称
    friend_name = "''"  # 替换为实际好友名称
    
    # 要发送的消息内容
    message = "你好，这是一条测试消息！"
    
    # 发送消息
    send_wx_msg(friend_name, message)